package com.commerce.product.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.commerce.common.entity.ProductReview;
import com.commerce.common.utils.PageResult;
import com.commerce.product.mapper.ReviewMapper;
import com.commerce.product.service.ReviewService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * 商品评价服务实现类
 */
@Slf4j
@Service
public class ReviewServiceImpl extends ServiceImpl<ReviewMapper, ProductReview> implements ReviewService {

    @Autowired
    private ReviewMapper reviewMapper;

    @Override
    public PageResult<ProductReview> getProductReviews(Long productId, int page, int size, Integer rating, Boolean withImages) {
        Page<ProductReview> pageParam = new Page<>(page, size);
        Page<ProductReview> pageResult = reviewMapper.selectProductReviews(pageParam, productId, rating, withImages);
        return PageResult.of(pageResult.getRecords(), pageResult.getTotal(), page, size);
    }

    @Override
    public Map<String, Object> getProductReviewStats(Long productId) {
        return reviewMapper.selectProductReviewStats(productId);
    }

    @Override
    @Transactional
    public boolean addReview(ProductReview review) {
        try {
            // 设置创建时间和更新时间
            review.setCreateTime(LocalDateTime.now());
            review.setUpdateTime(LocalDateTime.now());
            
            // 设置默认状态为已发布
            if (review.getStatus() == null) {
                review.setStatus(1);
            }
            
            // 设置默认非匿名
            if (review.getIsAnonymous() == null) {
                review.setIsAnonymous(0);
            }
            
            return save(review);
        } catch (Exception e) {
            log.error("添加商品评价失败", e);
            return false;
        }
    }

    @Override
    public PageResult<ProductReview> getUserReviews(Long userId, int page, int size) {
        Page<ProductReview> pageParam = new Page<>(page, size);
        Page<ProductReview> pageResult = reviewMapper.selectUserReviews(pageParam, userId);
        return PageResult.of(pageResult.getRecords(), pageResult.getTotal(), page, size);
    }

    @Override
    @Transactional
    public boolean replyReview(Long id, String replyContent) {
        try {
            ProductReview review = getById(id);
            if (review == null) {
                log.warn("评价不存在，ID: {}", id);
                return false;
            }
            
            review.setReplyContent(replyContent);
            review.setReplyTime(LocalDateTime.now());
            review.setUpdateTime(LocalDateTime.now());
            
            return updateById(review);
        } catch (Exception e) {
            log.error("回复评价失败", e);
            return false;
        }
    }

    @Override
    public boolean checkCanReview(Long userId, Long productId, Long orderId) {
        try {
            // 检查用户是否已经评价过该商品
            int count = reviewMapper.countUserProductReview(userId, productId, orderId);
            return count == 0;
        } catch (Exception e) {
            log.error("检查评价权限失败", e);
            return false;
        }
    }
}
